package action.admin.assignment;

import beans.Assignment;
import dao.MngAssignment;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class editAssign extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            String action = request.getParameter("action");
            if (action.equals("delete")) {
                String class_id = request.getParameter("class_id");
                String assign_name = request.getParameter("assign_name");
                MngAssignment mngAssign = new MngAssignment();
                List<Assignment> listAssign = mngAssign.showAssignByClassAndName(class_id, assign_name);
                for (Assignment assign : listAssign) {
                    mngAssign.deleteAssign(assign.getAssign_id());
                }
                String msg = "Delete successfull !";
                request.setAttribute("msg", msg);
                request.setAttribute("assign_name", assign_name);
                request.setAttribute("class_id", class_id);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=showassign");
                rd.forward(request, response);
            } else if (action.equals("update")) {
                String class_id = request.getParameter("class_id");
                String assign_name = request.getParameter("assign_name");
                MngAssignment mngAssign = new MngAssignment();
                List<Assignment> listAssign = mngAssign.showAssignByClassAndName(class_id, assign_name);
                Assignment assign = listAssign.get(1);
                String create_date = assign.getCreate_date();
                String finish_date = assign.getFinish_date();

                request.setAttribute("assign_name", assign_name);
                request.setAttribute("create_date", create_date);
                request.setAttribute("finish_date", finish_date);
                request.setAttribute("class_id", class_id);
                request.setAttribute("assign_name", assign_name);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=update_assign");
                rd.forward(request, response);
            } else if (action.equals("setmark")) {
                String assign_id = request.getParameter("assign_id");
                String assign_name = request.getParameter("assign_name");
                String mark = request.getParameter("mark");
                String class_id = request.getParameter("class_id");
                request.setAttribute("assign_id", assign_id);
                request.setAttribute("assign_name", assign_name);
                request.setAttribute("mark", mark);
                request.setAttribute("class_id", class_id);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=set_mark");
                rd.forward(request, response);
            } else if (action.equals("show_by_class")) {
                String class_id = request.getParameter("class_id");
                request.setAttribute("class_id", class_id);
                RequestDispatcher rd = request.getRequestDispatcher("index.jsp?page=showassign");
                rd.forward(request, response);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
